package com.example.dellpc.myapplication;

import android.app.Activity;
import android.content.pm.PackageManager;
import android.graphics.drawable.Animatable;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.controller.ControllerListener;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.image.ImageInfo;
import com.facebook.imagepipeline.image.QualityInfo;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.builder.PostFormBuilder;
import com.zhy.http.okhttp.callback.IDBRequestCallBack;

import java.io.Serializable;
import java.util.List;

import okhttp3.Call;


/**
 * Created by 刘智强 on 2016/8/29.
 */
public class OkHttpActivity extends Activity {
    private PostFormBuilder postFormBuilder;
    public static String URL = "http://indoorbuyapi.indoorbuy.com";
    public static String API = "/Api/Interface/indoorbuy.html"; // api
    // 接口地址
    public static String URL_PATH = URL + API;

    @ViewInject(R.id.text)
    private TextView textView;
    @ViewInject(R.id.imageview)
    private SimpleDraweeView simpleDraweeView;//所下载的图像可能和占位图尺寸不一致，如果设置出错图或者重试图的话，这些图的尺寸也可能和所下载的图尺寸不一致。
    // 如果大小不一致，假设使用的是 wrap_content，图像下载完之后，View将会重新layout，改变大小和位置。这将会导致界面跳跃。
    @ViewInject(R.id.pb)
    private ProgressBar progressBar;

    PackageManager packageManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Fresco.initialize(this);
        setContentView(R.layout.okhttpactivity);
        ViewUtils.inject(this);
        init();
        packageManager=getPackageManager();
    }

    public void init() {
        GcFirstClass(new BeanCallBack() {
            @Override
            public void onResponse(List<Bean> response) {
            }

            @Override
            public void inProgress(float progress) {
                Log.e(TAG, "inProgress:" + progress);
                progressBar.setVisibility(View.VISIBLE);
//                progressBar.setProgress((int)(100*progress));

            }

            @Override
            public void onRequstResponse(List<Bean> response, int code, String message) {
                Log.d("OkHttpActivity", code + "");
                Log.d("OkHttpActivity", response.toString());
                textView.setText(response.get(0).getGc_name());
                progressBar.setVisibility(View.GONE);

                DraweeController controller = Fresco.newDraweeControllerBuilder()
                        .setControllerListener(new ControllerListener<ImageInfo>() {
                            @Override
                            public void onSubmit(String id, Object callerContext) {

                            }

                            /**
                             * 加载完成
                             * @param id
                             * @param imageInfo
                             * @param animatable
                             */
                            @Override
                            public void onFinalImageSet(String id, ImageInfo imageInfo, Animatable animatable) {
                                QualityInfo qualityInfo = imageInfo.getQualityInfo();
                                Log.e("111",( qualityInfo.getQuality())/1024 + "KB");
                            }

                            /**
                             * 加载中。。。
                             * @param id
                             * @param imageInfo
                             */

                            @Override
                            public void onIntermediateImageSet(String id, ImageInfo imageInfo) {


                            }

                            @Override
                            public void onIntermediateImageFailed(String id, Throwable throwable) {

                            }

                            @Override
                            public void onFailure(String id, Throwable throwable) {

                            }

                            @Override
                            public void onRelease(String id) {

                            }


                        }).setUri(Uri.parse(response.get(0).getImage()))
                        .build();
                simpleDraweeView.setController(controller);

            }
        });


    }

    public void GcFirstClass(IDBRequestCallBack callBack) {
        PostFormBuilder postFormBuilder = OkHttpUtils.post().url(URL_PATH);
        postFormBuilder.putPostBody("InterfaceID", "GcFirstClass").build().execute(callBack);
    }

    class Bean implements Serializable {


        /**
         * gc_id : 1
         * gc_name : 妈咪宝贝
         * image : http://pic.indoorbuy.com/shop/common/category-pic-1.jpg
         */

        private String gc_id;
        private String gc_name;
        private String image;

        public String getGc_id() {
            return gc_id;
        }

        public void setGc_id(String gc_id) {
            this.gc_id = gc_id;
        }

        public String getGc_name() {
            return gc_name;
        }

        public void setGc_name(String gc_name) {
            this.gc_name = gc_name;
        }

        public String getImage() {
            return image;
        }

        public void setImage(String image) {
            this.image = image;
        }
    }

    class BeanCallBack extends IDBRequestCallBack<List<Bean>> {

        @Override
        public List<Bean> parseNetworkResponse(String data) throws Exception {
            List<Bean> beans = new Gson().fromJson(data, new TypeToken<List<Bean>>() {
            }.getType());
            return beans;
        }

        @Override
        public void onError(Call call, Exception e) {

        }

        @Override
        public void onResponse(List<Bean> response) {

        }

        @Override
        public void onRequstResponse(List<Bean> response, int code, String message) {

        }
    }
}
